package com.canaan.business.service.storehouse.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjectUtil;
import com.canaan.business.common.enums.storehouse.CheckStatusEnum;
import com.canaan.business.domain.storehouse.StockCheck;
import com.canaan.business.domain.vo.StockCheckDiffVo;
import com.canaan.business.service.storehouse.IStockCheckService;
import com.canaan.common.core.domain.AjaxResult;
import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.SecurityUtils;
import com.canaan.common.utils.TableNoUtil;
import com.canaan.common.utils.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.canaan.business.mapper.storehouse.StockCheckDiffMapper;
import com.canaan.business.domain.storehouse.StockCheckDiff;
import com.canaan.business.service.storehouse.IStockCheckDiffService;

/**
 * 库存盘点差异Service业务层处理
 *
 * @author ruoyi
 * @date 2023-02-14
 */
@Service
public class StockCheckDiffServiceImpl implements IStockCheckDiffService {
    @Resource
    private StockCheckDiffMapper stockCheckDiffMapper;
    @Resource
    private IStockCheckService stockCheckService;

    /**
     * 查询库存盘点差异
     *
     * @param id 库存盘点差异主键
     * @return 库存盘点差异
     */
    @Override
    public StockCheckDiff selectStockCheckDiffById(Long id) {
        return stockCheckDiffMapper.selectStockCheckDiffById(id);
    }

    /**
     * 查询库存盘点差异列表
     *
     * @param stockCheckDiff 库存盘点差异
     * @return 库存盘点差异
     */
    @Override
    public List<StockCheckDiff> selectStockCheckDiffList(StockCheckDiff stockCheckDiff) {
        List<StockCheckDiff> list = stockCheckDiffMapper.selectStockCheckDiffList(stockCheckDiff);
        if (stockCheckDiff.getIsDiff()!=null){
            if (stockCheckDiff.getIsDiff()==1){
                list = list.stream().filter(e->e.getStockQty().longValue()!=e.getCheckQty().longValue()).collect(Collectors.toList());
            }
        }
        return list;
    }

    /**
     * 新增库存盘点差异
     *
     * @param stockCheckDiff 库存盘点差异
     * @return 结果
     */
    @Override
    public int insertStockCheckDiff(StockCheckDiff stockCheckDiff) {
        if(StringUtils.isBlank(stockCheckDiff.getNo())){
            stockCheckDiff.setNo(TableNoUtil.getNo(StockCheckDiff.class));
        }
        stockCheckDiff.setCreateTime(DateUtils.getNowDate());
        stockCheckDiff.setCreateBy(SecurityUtils.getUsername());
        return stockCheckDiffMapper.insertStockCheckDiff(stockCheckDiff);
    }

    /**
     * 批量新增库存盘点差异
     *
     * @param stockCheckDiffList 库存盘点差异列表
     * @return 结果
     */
    @Override
    public int batchInsertStockCheckDiff(List<StockCheckDiff> stockCheckDiffList) {
        Date currerTime = DateUtils.getNowDate();
        String username = SecurityUtils.getUsername();
        for (StockCheckDiff stockCheckDiff : stockCheckDiffList){
            if(StringUtils.isBlank(stockCheckDiff.getNo())){
                stockCheckDiff.setNo(TableNoUtil.getNo(StockCheckDiff.class));
            }
            stockCheckDiff.setCreateTime(currerTime);
            stockCheckDiff.setCreateBy(username);
        }

        int insertNum = 0;
        for (int i=0; i<stockCheckDiffList.size();) {
            int endIndex = i+500;
            if (endIndex > stockCheckDiffList.size()) {
                endIndex = stockCheckDiffList.size();
            }
            insertNum = insertNum + stockCheckDiffMapper.batchInsertStockCheckDiff(stockCheckDiffList.subList(i, endIndex));
            i = endIndex;
        }
        return insertNum;
    }

    /**
     * 修改库存盘点差异
     *
     * @param stockCheckDiff 库存盘点差异
     * @return 结果
     */
    @Override
    public int updateStockCheckDiff(StockCheckDiff stockCheckDiff) {
        stockCheckDiff.setUpdateTime(DateUtils.getNowDate());
        stockCheckDiff.setUpdateBy(SecurityUtils.getUsername());
        return stockCheckDiffMapper.updateStockCheckDiff(stockCheckDiff);
    }

    /**
     * 批量修改库存盘点差异
     *
     * @param stockCheckDiffList 库存盘点差异列表
     * @return 结果
     */
    @Override
    public int batchUpdateStockCheckDiff(List<StockCheckDiff> stockCheckDiffList) {
        Date currerTime = DateUtils.getNowDate();
        String username = SecurityUtils.getUsername();
        for (StockCheckDiff stockCheckDiff : stockCheckDiffList){
            stockCheckDiff.setUpdateTime(currerTime);
            stockCheckDiff.setUpdateBy(username);
        }
        int updateNum = 0;
        for (int i=0; i<stockCheckDiffList.size();) {
            int endIndex = i+500;
            if (endIndex > stockCheckDiffList.size()) {
                endIndex = stockCheckDiffList.size();
            }
            updateNum = updateNum + stockCheckDiffMapper.batchUpdateStockCheckDiff(stockCheckDiffList.subList(i, endIndex));
            i = endIndex;
        }
        return updateNum;
    }

    /**
     * 批量删除库存盘点差异
     *
     * @param ids 需要删除的库存盘点差异主键集合
     * @return 结果
     */
    @Override
    public int deleteStockCheckDiffByIds(Long[] ids) {
        return stockCheckDiffMapper.deleteStockCheckDiffByIds(ids);
    }

    /**
     * 删除库存盘点差异信息
     *
     * @param id 库存盘点差异主键
     * @return 结果
     */
    @Override
    public int deleteStockCheckDiffById(Long id) {
        return stockCheckDiffMapper.deleteStockCheckDiffById(id);
    }

    /**
     * 查询库存盘点差异
     *
     * @param no 编号
     * @return 库存盘点差异
     */
    @Override
    public StockCheckDiff selectStockCheckDiffByNo(String no) {
        return stockCheckDiffMapper.selectStockCheckDiffByNo(no);
    }

    /**
     * 批量删除库存盘点差异
     *
     * @param nos 需要删除的编号集合
     * @return 结果
     */
    @Override
    public int deleteStockCheckDiffByNos(String[] nos) {
        return stockCheckDiffMapper.deleteStockCheckDiffByNos(nos);
    }

    /**
     * 删除库存盘点差异信息
     *
     * @param no 编号
     * @return 结果
     */
    @Override
    public int deleteStockCheckDiffByNo(String no) {
        return stockCheckDiffMapper.deleteStockCheckDiffByNo(no);
    }

    @Override
    public List<StockCheckDiff> selectStockCheckDiffListByCheckNo(String checkNO) {
        StockCheckDiff stockCheckDiff = new StockCheckDiff();
        stockCheckDiff.setCheckNo(checkNO);
        return this.selectStockCheckDiffList(stockCheckDiff);
    }

    @Override
    public AjaxResult queryCheckTaskDetails() {
        List status = Arrays.asList(CheckStatusEnum.CREATE.getCode(), CheckStatusEnum.WAIT.getCode());
        List<StockCheckDiff> list = stockCheckDiffMapper.queryCheckTaskDetails(status);
        return AjaxResult.success("查询成功", list);
    }

    @Override
    public AjaxResult queryCheckTaskCompletedDetails() {
        List status = Arrays.asList(CheckStatusEnum.COMPLETE.getCode());
        List<StockCheckDiff> list = stockCheckDiffMapper.queryCheckTaskDetails(status);
        return AjaxResult.success("查询成功", list);
    }

    @Override
    public AjaxResult queryCheckDetailInfos(StockCheckDiff stockCheckDiff) {
        List<StockCheckDiffVo> list = stockCheckDiffMapper.queryCheckDetailInfos(stockCheckDiff);
        return AjaxResult.success("查询成功", list);
    }

    @Override
    public AjaxResult checkComplete(List<StockCheckDiff> stockCheckDiffs) {
        if (CollUtil.isEmpty(stockCheckDiffs)) {
            return AjaxResult.error("失败");
        }
        for (StockCheckDiff stockCheckDiff : stockCheckDiffs) {
            stockCheckDiff.setStockStatus(CheckStatusEnum.COMPLETE.getCode());
            stockCheckDiffMapper.checkComplete(stockCheckDiff);
        }
        StockCheckDiff diff = stockCheckDiffMapper.selectStockCheckDiffById(stockCheckDiffs.get(0).getId());
        List<StockCheckDiff> list = stockCheckDiffMapper.queryCheckDiffNotComplete(diff.getCheckNo());
        if (StringUtils.isEmpty(list)) {
            StockCheck check = stockCheckService.selectStockCheckByNo(diff.getCheckNo());
            if (ObjectUtil.isNotEmpty(check)) {
                check.setStatus(CheckStatusEnum.COMPLETE.getCode());
                stockCheckService.updateStockCheck(check);
            }
        }
        return AjaxResult.success("成功");
    }
}
